// 队列相关类型定义

export interface JobData {
  id?: string;
  type: string;
  payload: any;
  priority?: number;
  delay?: number;
  attempts?: number;
  maxAttempts?: number;
  backoff?: BackoffStrategy;
  createdAt?: Date;
  processAt?: Date;
}

export interface Job extends JobData {
  id: string;
  status: JobStatus;
  createdAt: Date;
  processAt: Date;
  startedAt?: Date;
  completedAt?: Date;
  failedAt?: Date;
  error?: string;
  result?: any;
  progress?: number;
}

export type JobStatus = 'waiting' | 'active' | 'completed' | 'failed' | 'delayed' | 'paused';

export interface BackoffStrategy {
  type: 'fixed' | 'exponential' | 'linear';
  delay: number;
  maxDelay?: number;
  factor?: number;
}

export interface QueueConfig {
  defaultJobOptions: Partial<JobData>;
  concurrency: number;
  maxStalledCount: number;
  stalledInterval: number;
  retryProcessDelay: number;
}

export interface QueueStats {
  waiting: number;
  active: number;
  completed: number;
  failed: number;
  delayed: number;
  paused: number;
  total: number;
  throughput: {
    completed: number;
    failed: number;
    perMinute: number;
    perHour: number;
  };
}

export interface QueueMetrics {
  processingTime: {
    average: number;
    min: number;
    max: number;
    p95: number;
    p99: number;
  };
  jobTypes: Record<
    string,
    {
      count: number;
      averageTime: number;
      successRate: number;
    }
  >;
  errors: Array<{
    error: string;
    count: number;
    lastOccurred: Date;
  }>;
}

export interface WorkerConfig {
  concurrency: number;
  stalledInterval: number;
  maxStalledCount: number;
  processor: JobProcessor;
}

export type JobProcessor = (job: Job) => Promise<any>;

export interface QueueEvents {
  'job:waiting': (job: Job) => void;
  'job:active': (job: Job) => void;
  'job:completed': (job: Job, result: any) => void;
  'job:failed': (job: Job, error: Error) => void;
  'job:progress': (job: Job, progress: number) => void;
  'queue:paused': () => void;
  'queue:resumed': () => void;
  'queue:cleaned': (jobs: Job[], type: JobStatus) => void;
}
